package com.sgb.goods.viewmodel.inquiry;

import androidx.lifecycle.MutableLiveData;

import com.sgb.goods.api.GoodsCallBack;
import com.sgb.goods.api.GoodsNet;
import com.sgb.goods.entity.inquiry.InquirySearchSupplierEntity;
import com.sgb.goods.entity.inquiry.SupplierEntity;
import com.sgb.goods.utils.base.viewmodel.BaseViewModel;
import com.sgb.goods.view.activity.inquiry.FindInquirySupplierAdapter;
import com.sgb.goods.view.activity.inquiry.NewFindInquirySupplierAdapter;
import com.sgb.goods.view.adapter.inquiry.MyInquiryAdapter;
import com.swgk.core.base.api.APIManager;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 描述:询价发布-供应商列表
 *
 * @author qiangHeNoo
 * <p>
 * 时间： 2021.12.14
 */
public class FindInquirySupplierModel extends BaseViewModel {

    public MutableLiveData<List<SupplierEntity>> marketSupplierEntity = new MutableLiveData<>();
    public MutableLiveData<List<SupplierEntity>> vipMemberSupplierEntity = new MutableLiveData<>();
    public MutableLiveData<List<SupplierEntity>> memberSupplierEntity = new MutableLiveData<>();

    public int pageSize = 10;
    private int marketPageIndex = 1;
    private int vipMemberPageIndex = 1;
    private int memberPageIndex = 1;

    /***
     * 获取市场客户
     */
    public void getMarketSupplier(String searchContext, List<String> filterCompanyNos,boolean isLoadMore) {
        String longitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, "");
        String latitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, "");
        marketPageIndex = isLoadMore ? marketPageIndex + 1 : 1;
        InquirySearchSupplierEntity enquirySearchCompany = new InquirySearchSupplierEntity();
        enquirySearchCompany.setName(searchContext);
        enquirySearchCompany.setType(1);
        enquirySearchCompany.setLimit(pageSize);
        enquirySearchCompany.setPage(marketPageIndex);
        enquirySearchCompany.setLat(latitude);
        enquirySearchCompany.setLon(longitude);
        enquirySearchCompany.setFilterCompanyNos(filterCompanyNos);
        showLoading();
        GoodsNet.getInstance().getInquiryApi().getMemberCustomer(enquirySearchCompany).enqueue(new Callback<BaseEntity<ListResponse<List<SupplierEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<SupplierEntity>>>> call, Response<BaseEntity<ListResponse<List<SupplierEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null && APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    marketSupplierEntity.postValue(response.body().getData().getList());
                } else {
                    marketSupplierEntity.postValue(new ArrayList<>());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<SupplierEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                marketSupplierEntity.postValue(null);
            }
        });

    }

    /***
     * 获取会员客户
     */
    public void getVipMemberSupplier(String searchContext, List<String> filterCompanyNos,boolean isLoadMore) {
        String longitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, "");
        String latitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, "");
        vipMemberPageIndex = isLoadMore ? vipMemberPageIndex + 1 : 1;
        InquirySearchSupplierEntity enquirySearchCompany = new InquirySearchSupplierEntity();
        enquirySearchCompany.setName(searchContext);
        enquirySearchCompany.setType(2);
        enquirySearchCompany.setLimit(pageSize);
        enquirySearchCompany.setPage(vipMemberPageIndex);
        enquirySearchCompany.setLat(latitude);
        enquirySearchCompany.setLon(longitude);
        enquirySearchCompany.setFilterCompanyNos(filterCompanyNos);
        showLoading();
        GoodsNet.getInstance().getInquiryApi().getMemberCustomer(enquirySearchCompany).enqueue(new Callback<BaseEntity<ListResponse<List<SupplierEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<SupplierEntity>>>> call, Response<BaseEntity<ListResponse<List<SupplierEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null && APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    vipMemberSupplierEntity.postValue(response.body().getData().getList());
                } else {
                    vipMemberSupplierEntity.postValue(new ArrayList<>());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<SupplierEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                vipMemberSupplierEntity.postValue(null);
            }
        });
    }


    /***
     * 获取成员客户
     */
    public void getMemberSupplier(String searchContext, List<String> filterCompanyNos,boolean isLoadMore) {
        String longitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LONGITUDE_ID, "");
        String latitude = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LATITUDE_ID, "");
        memberPageIndex = isLoadMore ? memberPageIndex + 1 : 1;
        InquirySearchSupplierEntity enquirySearchCompany = new InquirySearchSupplierEntity();
        enquirySearchCompany.setName(searchContext);
        enquirySearchCompany.setType(3);
        enquirySearchCompany.setLimit(pageSize);
        enquirySearchCompany.setPage(memberPageIndex);
        enquirySearchCompany.setLat(latitude);
        enquirySearchCompany.setLon(longitude);
        enquirySearchCompany.setFilterCompanyNos(filterCompanyNos);
        showLoading();
        GoodsNet.getInstance().getInquiryApi().getMemberCustomer(enquirySearchCompany).enqueue(new Callback<BaseEntity<ListResponse<List<SupplierEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<SupplierEntity>>>> call, Response<BaseEntity<ListResponse<List<SupplierEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() != null && APIManager.RESPONSE_OK.equals(response.body().getState())) {
                    memberSupplierEntity.postValue(response.body().getData().getList());
                } else {
                    memberSupplierEntity.postValue(new ArrayList<>());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<SupplierEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                memberSupplierEntity.postValue(null);
            }
        });

    }
}
